<?php
//
//	file: installer/inc_advlog.php
//	begin: 08/05/2007
//	$Author: christopher $
//	$Revision: 27 $
//	$Date: 2008-01-28 22:20:14 +0000 (Mon, 28 Jan 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)

if ( !defined('IN_PHPBB') || !defined('IN_INSTALL') )
{
	die('Hack attempt !');
	exit;
}

class advlog_install
{
	function advlog_install()
	{
		global $parms_def;

		// includes
		include($config->url('includes/advlog/class_advlog'));
		lang_include('lang_CH_install_advlog');

		// parms (not set is : type => TYPE_INT, default => 0)
		$parms_def += array(
			'logpv' => array('type' => TYPE_NO_HTML, 'default' => ''),
			'ippv' => array('type' => TYPE_NO_HTML, 'default' => ''),
			'logd' => array('type' => TYPE_NO_HTML, 'default' => ''),
			'lomdi' => array('type' => TYPE_INT, 'default' => 0),
			'lomdl' => array('type' => TYPE_INT, 'default' => 0),
			'lond' => array('type' => TYPE_INT, 'default' => 0),
			'tlog' => array('type' => TYPE_INT, 'default' => 0),
			'clog' => array('type' => TYPE_INT, 'default' => 0),
		);

		// old tables
		define('OLD_LOGS_TABLE', $table_prefix . 'logs_actions');

		return;
	}

	function start()
	{
		global $part_script, $page;

		// parms (not set is : type => TYPE_INT, default => 0)
		$part_script[] = sprintf($page->lang('LOG_welcome_explain'), LOG_CURRENT_VERSION);

		return;
	}

	function prev_vers()
	{
		global $db, $page, $parms;

		// check
		$parms['logpv'] = '';
		$parms['ippv'] = '';

		// current line
		$sql = 'SELECT config_value
					FROM ' . CONFIG_TABLE . '
					WHERE config_name = \'mod_advanced_logging\'';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		if ( $row = $db->sql_fetchrow($result) )
		{
			$parms['logpv'] = $row['config_value'];
		}
		$db->sql_freeresult($result);

		// standalone line
		if ($parms['logpv'] == '')
		{
			$sql = 'SELECT config_value
					FROM ' . CONFIG_TABLE . '
					WHERE config_name = \'mod_log_actions\'';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			if ( $row = $db->sql_fetchrow($result) )
			{
				$parms['logpv'] = $row['config_value'];
			}
			$db->sql_freeresult($result);
		}

		if ($parms['logpv'])
		{
			$page->output(sprintf($page->lang('LOG_previous_version',$parms['logpv'])));
		}

		// ip tools standalone line
		$sql = 'SELECT *
					FROM ' . USERS_TABLE . '
					LIMIT 1';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		if ( $row = $db->sql_fetchrow($result) )
		{
			if($row['user_registered_ip'])
			{
				$parms['ippv'] = true;
			}
		}
		$db->sql_freeresult($result);

		if ($parms['ippv'])
		{
			$page->output(sprintf($page->lang('IP_previous_version')));
		}

		return;
	}

	function db()
	{
		global $schemas, $parms;

		// install schema
		$parms['lomdl'] = $parms['lomdi'] = false;
		switch ( $parms['logpv'] )
		{
			case '0.4.0':
			case '0.4.5':
			case '0.5.0':
			case '0.6.0':
			case '0.7.0':
			case '1.8.0':
			case '1.8.2':
			case '1.8.3':
				$schemas[] = 'advlog_190.xml';
				$parms['lomdl'] = true;
				if(!$parms['ippv'])
				{
					$schemas[] = 'advlog_iptools.xml';
					$parms['lomdi'] = true;
				}
				break;

			// new install
			default:
				$schemas[] = 'advlog_log.xml';
				if(!$parms['ippv'])
				{
					$schemas[] = 'advlog_iptools.xml';
					$parms['lomdi'] = true;
				}
				$parms['lond'] = true;
				break;
		}

		return;
	}

	function migrate()
	{
		global $parms, $page, $db, $config;

		if (!$parms['lomdi'] && !$parms['lomdl'])
		{
			return;
		}

		if ($parms['lomdl'])
		{
			$this->migrate_logs();
			$sql = 'DELETE FROM ' . CONFIG_TABLE . '
					WHERE config_name = \'mod_log_actions\'';
			$db->sql_query($sql, false, __LINE__, __FILE__);
			return;
		}

		if ($parms['lomdi'])
		{
			$this->migrate_iptools();
			return;
		}

		if ($parms['logd'])
		{
			$old_tables = explode(', ', $parms['logd']);
			$sql = 'DROP TABLE ' . $old_tables;
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		$page->output('LOG_migrate_succeed');

		return;
	}

	function migrate_logs()
	{
		global $parms, $page, $db;

		// check old logs table
		$sql = 'SELECT *
					FROM ' . OLD_LOGS_TABLE . '
					LIMIT 1';
		if ( $logs_table = ($result = $db->sql_query($sql, false, __LINE__, __FILE__, false)) )
		{
			$db->sql_freeresult($result);
		}

		// we have old logs
		if ( $logs_table )
		{
			if ( !intval($parms['tlog']) )
			{
				// clean new table
				$sql = 'TRUNCATE TABLE ' . LOGS_TABLE;
				$db->sql_query($sql, false, __LINE__, __FILE__);

				// count the existing logs
				$sql = 'SELECT COUNT(log_id) AS count_log_id
							FROM ' . OLD_LOGS_TABLE;
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$parms['tlog'] = ($row = $db->sql_fetchrow($result)) ? intval($row['count_log_id']) + 1 : 1;
				$db->sql_freeresult($result);
			}
			$total = intval($parms['tlog']) - 1;
			$start = intval($parms['clog']);
			$row_a_turn = intval(ROWS_A_TURN / 5);
			$logs = array();
			$sql = 'SELECT *
						FROM ' . OLD_LOGS_TABLE . '
						ORDER BY log_id
						LIMIT ' . intval($start) . ', ' . intval($row_a_turn);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$log_extras = unserialize($row['parm_extras']);
				//_dump($row);
				$log_data = array();
				switch ($row['log_type'])
				{
					case 'move':
						$log_data['new_forum'] = $log_extras['new_forum_id'];
					case 'lock':
					case 'unlock':
					case 'delete':
						$log_data['topics'] = array($row['log_parm_id'] => ($row['log_type'] == 'delete') ? $db->sql_escape_string($log_extras['topic_title']) : $this->get_topic_title($row['log_parm_id']));
						$row['forum_id'] = ($row['log_type'] == 'delete') ? 0 : (($row['log_type'] == 'move') ? $log_extras['old_forum_id'] : $this->get_forum_id($row['log_parm_id']));
						break;
					case 'editpost':
						$log_data['post_id'] = $row['log_parm_id'];
					case 'deletepost':
						$log_data['topic_id'] = ($row['log_type'] == 'deletepost') ? $row['log_parm_id'] : $this->get_topic_id($row['log_parm_id']);
						$log_data['topic_title'] = $this->get_topic_title($log_data['topic_id']);
						$row['forum_id'] = $this->get_forum_id($log_data['topic_id']);
						break;
					case 'split':
						$log_data['new_forum_id'] = 0;
						$log_data['new_topic_id'] = 0;
						$log_data['old_topic_id'] = intval($row['log_parm_id']);
						$log_data['new_topic_title'] = '';
						$log_data['old_topic_title'] = $this->get_topic_title($row['log_parm_id']);
						$log_data['posts'] = array();
						$row['forum_id'] = $this->get_forum_id($row['log_parm_id']);
						break;
				}
				$logs[ intval($row['log_id']) ] = array(
					'log_type' => trim($row['log_type']),
					'log_time' => trim($row['log_time']),
					'log_user_id' => trim($row['log_user_id']),
					'log_user_ip' => ($log_extras['user_ip']) ? trim($log_extras['user_ip']) : '',
					'log_forum_id' => ($row['forum_id']) ? trim($row['forum_id']) : '',
					'log_data' => serialize($log_data),
				);
			}
			$db->sql_freeresult($result);
			$count_logs = count($logs);

			// create the logs in the new table
			if ( $count_logs )
			{
				foreach ( $logs as $old_log_id => $fields )
				{

					$sql = 'INSERT INTO ' . LOGS_TABLE . '
								(' . $db->sql_fields('fields',$fields); . ') VALUES(' . $db->sql_fields('values',$fields); . ')';
					$db->sql_query($sql, false, __LINE__, __FILE__);
				}
			}
			unset($logs);

			$parms['clog'] = (intval($parms['clog']) ? intval($parms['clog']) : 0) + $count_logs;
			$total_done = intval($parms['tlog']) - 1 - intval($parms['clog']);
			$percent_done = max(1, min(100, round((100 * $total_done) / max(1, intval($parms['tlog']) - 1))));

			// send percent box
			percent_box(sprintf($page->lang('LOG_log_percent'), intval($parms['clog']), intval($parms['tlog']) - 1), $percent_done);
			if ( $count_logs )
			{
				$page->loop($parms);
			}
		}
		$parms['old_tables'] = OLD_LOGS_TABLE;

		$page->output('LOG_old_logs_succeed');

		return;
	}

	function migrate_iptools()
	{
		global $parms, $page, $db, $config;

		// to-do

		$page->output('LOG_old_ip_succeed');

		return;
	}


	function get_topic_title($topic_id)
	{
		global $db;
		$return = '';
		$sql = 'SELECT  topic_title FROM ' . TOPICS_TABLE . '
			WHERE topic_id = ' . $topic_id;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$return = ($row = $db->sql_fetchrow($result)) ? ($row['topic_title']) : '';
		$db->sql_freeresult($result);
		return $db->sql_escape_string($return);
	}

	function get_topic_id($post_id)
	{
		global $db;
		$return = 0;
		$sql = 'SELECT topic_id FROM ' . POSTS_TABLE
			. ' WHERE post_id = ' . $post_id;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$return = ($row = $db->sql_fetchrow($result)) ? ($row['topic_id']) : '';
		$db->sql_freeresult($result);
		return intval($return);
	}

	function get_forum_id($topic_id)
	{
		global $db;
		$return = 0;
		$sql = 'SELECT forum_id FROM ' . TOPICS_TABLE
			. ' WHERE topic_id = ' . $topic_id;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$return = ($row = $db->sql_fetchrow($result)) ? ($row['forum_id']) : '';
		$db->sql_freeresult($result);
		return intval($return);
	}

	function init_data()
	{
		global $parms, $db, $page, $config, $colors;

		if ( !$parms['lond'] )
		{
			return;
		}

		// attempt to determine original ip address of users

		return;
	}

	function patch()
	{
		global $config;

		$config->set('mod_advanced_logging', LOG_CURRENT_VERSION, true);

		return;
	}

	function fin()
	{
		global $page, $parms;

		$page->output(empty($parms['logpv']) ? 'LOG_install_done' : 'LOG_install_upgraded');

		return;
	}
}

$parts[] = 'advlog_install';

?>